let http = require('http')
let fs = require('fs');
let urlparse = require('url')

http.createServer(function (req, res) {
    let name = urlparse.parse(req.url, true).query.name;

    if (name === undefined) name = 'world';

    if (name == 'burningbird') {
        let file = '../images/leno_0103.png';
        fs.stat(file, function (err, stat) {
            if (err) {
                console.error(err)
                res.writeHead(200, {'Content-Type': 'text/plain'});
                res.end("Sorry, Burningbird isn't around right now \n");
            } else {
                let img = fs.readFileSync(file);
                res.contentType = 'img/png';
                res.contentLength = stat.size;
                res.end(img, 'binary')
            }
        });
    } else {
        res.writeHead(200, {
            'Content-Type': 'text/plain'
        });
        res.end('Hello ' + name + ' World\n');
    }


}).listen(8124);


console.log('Server running at http://localhost:8124');